<template>
  <el-header :style="'margin-bottom:' + headerBottom + 'px'">
    <!--Blog-->
    <h2 class="logo"><svg-icon icon-class="EarOfWheat" /> Blog</h2>
    <div class="bg-purple-light">
      <el-menu :default-active="activeIndex"
               router
               class="el-menu-demo"
               mode="horizontal"
               style="border: none;"
               background-color="rgba(0,0,0,0)"
               text-color="#fff"
               active-text-color="#ffd04b"
      >
        <el-menu-item index="/cms/main/cmsIndex">
          <i class="el-icon-s-home" style="color: rgba(255, 255, 255,255);"></i>
          首页
        </el-menu-item>
        <el-menu-item :index="item.path" v-for="item in menulist" :key="item.id">
          <!-- 图标 -->
          <i :class="item.icon" style="color: rgba(255, 255, 255,255);"></i>
          <!-- 文本 -->
          {{ item.authName }}
        </el-menu-item>
      </el-menu>
    </div>
    <div class="bg-purple-light el-menu-hidden"
         v-if="menuHiddenVisiable">
      <el-menu :default-active="activeIndex"
               router
               background-color="rgba(84,92,100,0.5)"
               text-color="#fff"
               active-text-color="#ffd04b"
      >
        <el-menu-item index="/cms/main/cmsIndex" @click="menuAway">
          <i class="el-icon-s-home" style="color: rgba(255, 255, 255,255);"></i>
          首页
        </el-menu-item>
        <el-menu-item :index="item.path" v-for="item in menulist" :key="item.id" @click="menuAway">
          <!-- 图标 -->
          <i :class="item.icon" style="color: rgba(255, 255, 255,255);"></i>
          <!-- 文本 -->
          {{ item.authName }}
        </el-menu-item>
      </el-menu>
    </div>

    <div class="menu-expend" @click="menuExpend">
      <i class="el-icon-menu" style="color: rgba(255, 255, 255,255);"></i>
    </div>

    <div v-if="searchInput" class="search_input">
      <el-input
          @focus="checkInput"
          @blur="notSearching()"
          class="search"
          placeholder="搜索博客"
          prefix-icon="el-icon-search"
          v-model="queryInfo.query"
          size="mini">
      </el-input>
      <ul v-if="searching">
        <li
            class="animate__animated animate__fadeInDown search-blog"
            v-for="blog in searchList"
            :key="blog.id"
            @click="getBlogInfo(blog.id)"
        >
          <a><span v-html="blog.title"></span></a>
        </li>
      </ul>
    </div>

    <div v-if="islogin" class="bg-purple">
      <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">
        <div class="avatar-wrapper">
          <el-avatar class="user-avatar" :src="avatar" @error="errorHandler">
            <i class="el-icon-s-custom" />
          </el-avatar>
          <p class="avatar-Name">{{ name }}</p>
        </div>
        <el-dropdown-menu slot="dropdown">
          <router-link target="_blank" to="/index">
            <el-dropdown-item>管理博客</el-dropdown-item>
          </router-link>
          <el-dropdown-item divided @click.native="logout">
            <span>退出登录</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <div v-else class="bg-purple">
      <div class="avatar-wrapper">
        <i class="el-icon-s-custom" @click="tologin"/>
        <p class="avatar-Name" @click="tologin">登录|注册</p>
      </div>
    </div>
  </el-header>
</template>

<script setup>

import useUserStore from "@/store/modules/user";
const userStore = useUserStore();

import { cmsListBlog } from '@/api/cms/blog';
import { getToken } from '@/utils/auth';

import { useRouter } from 'vue-router';

const menulist = ref([
  {
    id: 1,
    authName: '随笔',
    path: '/cms/main/essay',
    icon: 'el-icon-edit',
  },
  {
    id: 2,
    authName: '留言',
    path: '/cms/main/message',
    icon: 'el-icon-chat-dot-round',
  },
  {
    id: 3,
    authName: '文档',
    path: '/cms/doucument',
    icon: 'el-icon-document',
  },
]);

const queryInfo = ref({
  query: '',
  timer: null,
});
const router = useRouter();
const searchList = ref([]);
const searching = ref(false);

const activeIndex = ref(router.path);
const islogin = ref(false);
const searchInput = ref(true);
const menuHiddenVisiable = ref(false);
const headerBottom = ref(0);
//const store = useStore();
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  title: null,
  type: 1,
  content: null,
  top: null,
  views: null,
  status: null,
});


watch(queryInfo.query, (value) => {
  if (queryInfo.timer) {
    clearTimeout(queryInfo.timer);
  }
  queryInfo.timer = setTimeout(() => {
    searchBlog();
  }, 300);
});






function menulistAdd() {
  menulist.value.unshift({
    id: '',
    authName: '',
  });
}

function menuExpend() {
  menuHiddenVisiable.value = !menuHiddenVisiable.value;
  headerBottom.value = menuHiddenVisiable.value
      ? (menulist.value.length + 1) * 56
      : 0;
}

function menuAway() {
  menuHiddenVisiable.value = false;
  headerBottom.value = 0;
}

function notSearching() {
  setTimeout(() => {
    searching.value = false;
  }, 100);
}

function checkInput() {
  searching.value = queryInfo.query !== '';
}

function tologin() {
  router.push({ path: "/cmsLogin" });
}

function login() {
  const token = getToken();
  if (token == null || token == '') {
    islogin.value = false;
  } else {
    islogin.value = true;
  }
}

function errorHandler() {
  return true;
}

async function searchBlog() {
  if (!queryInfo.query === '') {
    searching.value = false;
    return;
  }
  queryParams.title = queryInfo.query;
  cmsListBlog(queryParams).then(response => {
    let lsitSize = response.rows.length;
    if (lsitSize > 0) {
      for (let i = 0; i < lsitSize; i++) {
        let redTitle = brightenKeyword(response.rows[i].title, queryInfo.query);
        response.rows[i].title = redTitle;
      }
    }
    searchList.value = response.rows;
    if (searchList.value.length !== 0) {
      searching.value = true;
    }
  });
}

function brightenKeyword(val, keyword) {
  const Reg = new RegExp(keyword, 'i');
  let res = '';
  if (val) {
    res = val.replace(Reg, `<span style="color: red;">${keyword}</span>`);
    return res;
  }

}

function getBlogInfo(blogId) {
  const routeUrl = router.resolve({
    path: '/cms/main/blog',
    query: {
      id: blogId,
    },
  });
  window.open(routeUrl.href, '_blank');
}

 async function logout() {
  try {
    await router.confirm('确定注销并退出系统登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    });
    await userStore.logOut();
    location.href = '/cms/main/cmsIndex';
  } catch (error) {
    console.error(error);
  }
}

</script>

<style scoped>
.el-header {
  display: flex;
  justify-content: space-between;
  background-color: rgba(0, 0, 0, 0.2);
  align-items: center;
  transition: 0.2s;
}

.el-header:hover {
  opacity: 1 !important;
}

.el-menu {
  flex-shrink: 0;
  background-color: rgba(0, 0, 0, 0) !important;
}
:deep (.el-menu-item) {
  background-color: rgba(0, 0, 0, 0) !important;
}

:deep (.el-menu-item) i {
  color: rgba(255, 255, 255,255);
}

:deep (.el-menu-item:hover) i {
  color: white;
}

 :deep (.el-menu-item:hover) {
  background-color: rgba(0, 0, 0, 0.5) !important;
}

.search_input {
  position: relative;
  box-sizing: border-box;
}

.search_input ul {
  position: absolute;
  top: 30px;
  width: 100%;
  border: 1px solid #e5e9ef;
  margin-top: 1px;
  background: #fff;
  z-index: 10000;
  border-radius: 2px;
  box-shadow: 0 2px 4px rgba(58, 118, 142, 0.16);
  padding: 10px 0;
  font-size: 14px;
  box-sizing: border-box;
}

.search_input ul li {
  padding: 0 16px;
  height: 32px;
  line-height: 32px;
  cursor: pointer;
  word-wrap: break-word;
  word-break: break-all;
  display: block;
  color: #222;
  position: relative;
  /*transition: .2 ease;*/
}

.search_input ul li:hover {
  background-color: #e8f3ff;
}

.search-blog {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  padding-left: 20px;
  padding-right: 20px;
}

.bg-purple-light {
  float: right;
}

.bg-purple {
  float: right;
}

/*.user-avatar {
  float: left;
  cursor: pointer;
  border: dashed rgba($color: #ffff7f, $alpha: 0.5);
}*/
.user-avatar {
  float: left;
  cursor: pointer;
  border: dashed rgba(255, 255, 127, 0.5);
}


.avatar-container {
  margin-right: 30px;
}

.avatar-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.avatar-Name {
  margin-left: 10px;
  cursor: pointer;
  float: right;
  font-size: 16px;
  color: #ffffff;
}

.logo {
  float: left;
  color: #ffd04b;
  font-weight: bold;
}

.logo:hover {
  cursor: pointer;
}

.el-menu-hidden {
  /*display: none;*/
  position: absolute;
  top: 60px;
  left: 0;
  border-top: 1px solid #ccc;
  border-right: none;
  width: 100%;
}

.menu-expend {
  display: none !important;
}

/* 窗口可视区域小于1000隐藏搜索框 */
@media screen and (max-width: 1000px) {
  .search_input {
    display: none;
  }
}

@media screen and (max-width: 768px) {
 :deep (.el-menu-item) {
    background-color: rgba(0, 0, 0, 0.3) !important;
  }

  .el-menu-demo {
    display: none;
  }

  .menu-expend {
    display: block !important;
    float: right;
  }

  .menu-expend:hover {
    color: #ffd04b;
    cursor: pointer;
  }
}
</style>
